<?php

// +----------------------------------------------------------------------

// | Description: 客户

// +----------------------------------------------------------------------

// | Author: zl

// +----------------------------------------------------------------------



namespace app\crm\controller;



use app\admin\controller\ApiCommon;

use app\crm\model\BalanceLog;

use app\crm\model\Setting;

use app\crm\model\SettingCategory;

use app\crm\model\Service;

use app\crm\model\ReturnVist;

use app\crm\model\ReturnVistT;

use app\crm\model\SendMsgModel;

use app\crm\model\Customer as CustomerModel;

use app\crm\model\TransferShop;

use app\crm\model\CustomerConfig as CustomerConfigModel;

use app\crm\model\CustomerGrade as CustomerGradeModel;

use app\crm\model\ReserveandLost;

use think\response\Json;

use think\facade\Db;

use think\facade\Request;

use app\admin\model\Coupon as CouponModel;

use app\common\traits\ActionRecordTrait;

use app\shop\model\unionpay\AppConfig;
use app\shop\model\unionpay\AppUtil;
use app\shop\model\unionpay\Test;
use app\crm\model\MiniProgramModel;
use app\shop\model\RefundPayModel;


class ExportLink extends ApiCommon
{
    protected $customerModel;

    protected $customerConfigModel;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @other 其他根据系统设置

     **/

    public function initialize()

    {

        $action = [

            'permission' => ['du_link_1'],

            'allow' => ['users', 'info']

        ];

        event('CheckAuth',$action);

        $request = $this->request;

        $a = strtolower($request->action());



        $this->customerModel = new CustomerModel();

        $this->customerConfigModel = new CustomerConfigModel();

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    public function du_link_1()
    {

        $request = request()->param();

        // echo "<pre>";
        // print_r($request);
        // echo "</pre>";
        // exit;

        $start_time = strtotime($request['start_time']);
        $end_time = strtotime($request['end_time'])+86399;

        $where = [];

        array_push($where,['type','<>','2']);
        array_push($where,['shop_name','<>','营销中心']);
        array_push($where,['status','=','1']);

        $shop_list = DB::name('shop')->where($where)->column('shop_id');

        

        $where = [];

        array_push($where,['balance_category_id','=','4']);
        array_push($where,['shop_id','in',$shop_list]);

        $balance_list_b = Db::name('customer_balance')->where($where)->group('shop_id')->column('SUM(money)','shop_id');

        $balance_list_z = Db::name('customer_balance')->where($where)->group('shop_id')->column('SUM(complimentary_money)','shop_id');

        $where = [];
        array_push($where,['business_type','=','4']);
        array_push($where,['shop_id','in',$shop_list]);
        array_push($where,['type','=','3']); // 收款单
        array_push($where,['refund_type','=','101.02']);
        array_push($where,['status','=','2']);
        array_push($where,['is_pay','=','1']);
        array_push($where,['deal_time','between',"$start_time,$end_time"]);

        $refund_order_s = Db::name('shop_refund_order')->where($where)->group('shop_id')->column('SUM(pay_amount)','shop_id');

        array_push($where,['type','=','1']); // 退款单

        $refund_order_t = Db::name('shop_refund_order')->where($where)->group('shop_id')->column('SUM(pay_amount)','shop_id');

        $where = [];
        array_push($where,['business_type','=','4']);
        array_push($where,['shop_id','in',$shop_list]);
        array_push($where,['status','=','2']);
        array_push($where,['is_pay','=','1']);
        array_push($where,['deal_time','between',"$start_time,$end_time"]);

        $order_s = Db::name('shop_order')->where($where)->group('shop_id')->column('SUM(order_amount)','shop_id');

        $where = [];
        array_push($where,['business_type','=','4']);
        array_push($where,['shop_id','in',$shop_list]);
        array_push($where,['type','=','2']); // 退货单
        array_push($where,['refund_type','=','101.02']);
        array_push($where,['status','=','2']);
        array_push($where,['is_pay','=','1']);
        array_push($where,['deal_time','between',"$start_time,$end_time"]);

        $order_t = Db::name('shop_refund_order')->where($where)->group('shop_id')->column('SUM(pay_amount)','shop_id');

        // echo "<pre>";
        // print_r($order_t);
        // echo "</pre>";
        // exit;

        $shop_name = DB::name('shop')->where('shop_id','in',$shop_list)->column('shop_name','shop_id');

        $list = [];

        foreach ($shop_list as $key => $value) {

            // echo $value;
            // echo "<br/>";
            
            $one = [];

            $one['shop_name'] = $shop_name[$value] ?? '';
            $one['bj'] = $balance_list_b[$value] ?? '';
            $one['zj'] = $balance_list_z[$value] ?? '';

            $sk = $refund_order_s[$value] ?? '0.00';
            $tk = $refund_order_t[$value] ?? '0.00';

            $one['ss'] = bcsub($sk,$tk,2);

            $sc = $order_s[$value] ?? '0.00';
            $th = $order_t[$value] ?? '0.00';

            $one['xs'] = bcsub($sc,$th,2);

            array_push($list,$one);
            
        }

        $callName = [
            'shop_name' => '门店',
            'bj' => '本金',
            'zj' => '增金',
            'ss' => '收款',
            'xs' => '出库',
        ];

        $downUrl = exportExcelData('护肤品数据导出',$callName,$list,'xlsx',1);

        echo "<script language='javascript'>";

        echo " location='".$downUrl."';";

        echo "</script>";
    }

}